char r[1001];main(c,i,b,d,s,u,e,q,w){for(gets(r);~scanf("%s\n",r);b|=s|d|e,puts(b?"ILLEGAL":"LEGAL")){for(i=b=d=s=u=e=q=w=0;!b&&(c=r[i]);i++){b|=(c=='.')&&(w|q|!u);b|=((c=='e')|(c=='E'))&&(s|d|e|q|!u);b|=((c=='+')|(c=='-'))&&(s|d|u);e=c=='e'|c=='E';q|=e;d=c=='.';w|=d;u=c>47&&c<58;s=c=='+'|c=='-';b|=!(e|d|u|s);}}}
